<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="140px"
      @keyup.enter.native="dataFormSubmit()"
    >
      <el-form-item label="设备型号" prop="equipmentType">
        <el-input v-model="dataForm.equipmentType" />
      </el-form-item>
      <el-form-item label="脑电导联数" prop="leadNumber">
        <el-input v-model="dataForm.leadNumber" />
      </el-form-item>
      <!--      <el-form-item label="电极坐标分布" prop="electrodeCoordinateDistribution">-->
      <!--        <el-input v-model="dataForm.electrodeCoordinateDistribution" ></el-input>-->
      <!--      </el-form-item>-->
      <el-form-item label="眼电电极" prop="eyeElectrodes">
        <el-input v-model="dataForm.eyeElectrodes" />
      </el-form-item>
      <el-form-item label="心电电极" prop="electrocardioElectrode">
        <el-input v-model="dataForm.electrocardioElectrode" />
      </el-form-item>
      <el-form-item label="电极材料" prop="electrodeMaterial">
        <el-input v-model="dataForm.electrodeMaterial" />
      </el-form-item>
      <el-form-item label="描述" prop="description" >
        <el-input v-model="dataForm.description" type="textarea" rows="3"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import device from '@/api/device'
import myUtils from '../../api/myUtils'
export default {
  data() {
    return {
      visible: true,
      dataRule: {
        equipmentType: [{ required: true, message: '设备名字不能为空', trigger: 'blur' }],
        leadNumber: [{ required: true, message: '脑电导联数不能为空', trigger: 'blur' }],
        // electrodeCoordinateDistribution: [{ required: true, message: "电极坐标分布不能为空", trigger: "blur" }],//@后续考虑不要电极坐标分布
        eyeElectrodes: [{ required: true, message: '眼电电极不能为空，诺没有则填写无', trigger: 'blur' }],
        electrocardioElectrode: [{ required: true, message: '心电电极不能为空，诺没有则填写无', trigger: 'blur' }],
        electrodeMaterial: [{ required: true, message: '电极材料不能为空', trigger: 'blur' }]
      },

      dataForm: {
        equipmentType: null,
        id: 0
      }

    }
  },
  methods: {
    init(id) {
      this.dataForm = {}
      this.visible = true
      this.$refs['dataForm'].resetFields()

      if (id != 0) {
        device.getById(id).then((response) => {
          if (response.code == '000000') {
            this.dataForm = response.data
          } else {
            this.$message.error('获取数据失败')
          }
        })
      }
      this.dataForm.id = id || 0 // 这一句其实可有可无的

      this.dataForm.sitePkId = myUtils.getSite(this.$route.query.subjectPkId, this.$store.getters.roles)
      // this.dataForm.siteName=this.$store.getters.sitename;
      this.dataForm.createPin = this.$store.getters.name
    },

    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          let myDate = new Date();
          this.dataForm.createTime = myDate.toLocaleString();
          //todo 这里要改一下
          // var strTime = '2011-04-16'
          // var date = new Date(Date.parse(strTime.replace(/-/g, '/')))
          // this.dataForm.createTime = date
          var formatDate = function (date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            var d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            return y + '-' + m + '-' + d;
          };
          this.dataForm.createTime = formatDate(new Date())
          if (this.dataForm.id == 0) {
            device.equipment(this.dataForm).then(({ code }) => {
              if (code === '000000') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 700,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error("操作失败")
              }
            })
          } else {
            device.updateEquipment(this.dataForm).then(({ code }) => {
              if (code === '000000') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 700,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error("操作失败")
              }
            })
          }
        }
      })
    }
  }

}
</script>
<style>

</style>
